package com.xuemeng.web;


import com.github.pagehelper.PageHelper;
import com.xuemeng.entity.Tdept;
import com.xuemeng.model.JqgridBean;
import com.xuemeng.model.PageRusult;
import com.xuemeng.service.TdeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @author xuemeng
 * @date 2018/12/12 22:03
 * 部门管理操作类
 */
@Controller
@RequestMapping("/admin/dept")
public class DeptAdminController {

        @Resource
        private TdeptService tdeptService;

        /**
         * 进入部门管理主页面
         *
         * @return
         */
        @RequestMapping("/todeptmanage")
        @RequiresPermissions(value = {"部门管理"})
        public String tousermanage() {

                return "power/dept";
        }

        @ResponseBody
        @RequestMapping(value = "/list")
        @RequiresPermissions(value = {"部门管理"})
        public Map<String, Object> list(JqgridBean jqgridbean
                        /*String userName,@RequestParam(value="page",required=false)Integer page*/) throws Exception {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                LinkedHashMap<String, Object> datamap = new LinkedHashMap<String, Object>();

                Example tdeptExample = new Example(Tdept.class);
                //tuserExample.or().andIdNotEqualTo(1L);
                Example.Criteria criteria = tdeptExample.or();

                if (StringUtils.isNotEmpty(jqgridbean.getSearchField())) {
                        if ("name".equalsIgnoreCase(jqgridbean.getSearchField())) {
                                        criteria.andLike("name", "%"+jqgridbean.getSearchString()+"%");
                        }
                }

                PageHelper.startPage(jqgridbean.getPage(), jqgridbean.getLength());
                List<Tdept> deptList = tdeptService.selectByExample(tdeptExample);
                PageRusult<Tdept> pageRusult = new PageRusult<Tdept>(deptList);

                resultmap.put("currpage", String.valueOf(pageRusult.getPageNum()));
                resultmap.put("totalpages", String.valueOf(pageRusult.getPages()));
                resultmap.put("totalrecords", String.valueOf(pageRusult.getTotal()));
                resultmap.put("datamap", deptList);

                return resultmap;
        }


        @ResponseBody
        @RequestMapping(value = "/addupdatedept")
        @RequiresPermissions(value = {"部门管理"})
        public Map<String, Object> addupdatedept(Tdept tdept) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tdept.getId() == null) {//新建
                                //首先判断用户名是否可用
                                Example tdeptExample = new Example(Tdept.class);
                                tdeptExample.or().andEqualTo("name", tdept.getName());
                                List<Tdept> rolelist = tdeptService.selectByExample(tdeptExample);
                                if (rolelist != null && rolelist.size() > 0) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前角色名已存在");
                                        return resultmap;
                                }
                                tdeptService.saveNotNull(tdept);
                        } else {//编辑
                                Tdept oldObject = tdeptService.selectByKey(tdept.getId());
                                if (oldObject == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前角色名不存在");
                                        return resultmap;
                                } else {
                                        tdeptService.updateNotNull(tdept);
                                }
                        }
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "操作成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "操作失败，系统异常");
                        return resultmap;
                }
        }


        @ResponseBody
        @RequestMapping(value = "/deletedept")
        @RequiresPermissions(value = {"部门管理"})
        public Map<String, Object> deletedept(Tdept tdept) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tdept.getId() != null && !tdept.getId().equals(0)) {
                                tdept = tdeptService.selectByKey(tdept.getId());
                                if (tdept == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "删除失败,无法找到该记录");
                                        return resultmap;
                                } else {

                                        tdeptService.delete(tdept.getId());
                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "删除失败");
                        }


                        resultmap.put("state", "success");
                        resultmap.put("mesg", "删除成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "删除失败，系统异常");
                        return resultmap;
                }
        }

        @ResponseBody
        @RequestMapping(value = "/selectDeptById")
        @RequiresPermissions(value = {"部门管理"})
        public Map<String, Object> selectDeptById(Tdept tdept) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tdept.getId() != null && !tdept.getId().equals(0)) {
                                tdept = tdeptService.selectByKey(tdept.getId());
                                if (tdept == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "无法找到该记录");
                                        return resultmap;
                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "无法找到该记录的id");
                                return resultmap;
                        }

                        resultmap.put("tdept", tdept);
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "获取成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "获取失败，系统异常");
                        return resultmap;
                }
        }



}
